package com.guochenglang.www.controller;

import com.guochenglang.www.pojo.Comment;
import com.guochenglang.www.pojo.User;
import com.guochenglang.www.service.CommentService;
import com.guochenglang.www.service.UserService;
import com.guochenglang.www.util.GetObject;
import com.guochenglang.www.util.MyDependency;
import com.guochenglang.www.util.RequestReCodeUtil;
import com.guochenglang.www.view.vo.ArticleCommentView;
import com.guochenglang.www.view.vo.CommentView;
import com.guochenglang.www.web.result.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;

/**
 * 评论Controller层
 *
 * @author GuoChenglang
 */

public class CommentController {
    private static final CommentService COMMENT_SERVICE = MyDependency.returnObject(CommentService.class);
    private static final UserService USER_SERVICE = MyDependency.returnObject(UserService.class);

    /**
     * 获得文章所有评论
     */
    public static Result getComment(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获取文章id
        int articleId = Integer.parseInt(request.getParameter("articleId"));
        //搜索评论
        Comment commentCondition = new Comment();
        commentCondition.setArticleId(articleId);
        ArrayList<Comment> comments = COMMENT_SERVICE.getComment(commentCondition);
        ArrayList<ArticleCommentView> articleCommentViews = new ArrayList<>();
        for (Comment comment : comments) {
            //获得评论、已经评论的用户名，一同返回前端显示
            ArticleCommentView articleCommentView = new ArticleCommentView();
            articleCommentView.setComment(comment);
            User user = USER_SERVICE.getUser(comment.getCommentId());
            articleCommentView.setUsername(user.getUsername());
            articleCommentViews.add(articleCommentView);
        }
        return Result.result(articleCommentViews);

    }

    /**
     * 删除评论
     */
    public static Result deleteComment(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, IOException {
        //获得评论的id
        int id = GetObject.getObject(request, Integer.class);
        //根据id删除评论
        int num = COMMENT_SERVICE.deleteComment(id);
        return Result.result(num);
    }

    /**
     * 进行评论
     */
    public static Result inputComment(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //编码设置
        RequestReCodeUtil.reCode(request, response);
        //获得userId
        int userId = GetObject.getValue(request, Integer.class, "userId");
        //获得评论内容
        String comment = GetObject.getValue(request, String.class, "comment");
        //获得文章的id
        int articleId = GetObject.getValue(request, Integer.class, "articleId");
        //封装进Comment对象
        Comment myComment = new Comment(null, articleId, null, userId, comment);
        //设置评论时间
        System.out.println(myComment);
        myComment.setCommentTime(new Timestamp(System.currentTimeMillis()));
        //添加评论
        int key = COMMENT_SERVICE.comment(myComment);
        if (key < 0) {
            return Result.dangerWord();
        }
        return Result.result(key);
    }

}
